package com.chilly.jdkNio.action;

import io.netty.util.CharsetUtil;

import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

/**
 * 应用实例1：本地文件写数据
 * 将hello写入到filte01.txt中
 * Created by Chilly Cui on 2021/1/9.
 */
public class NIOFileChannelOper01 {
    public static void main(String[] args) throws IOException {
        String str = "hello 北京";
        FileOutputStream fileOutputStream = new FileOutputStream("c:\\env\\file01.txt");

        FileChannel channel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        byteBuffer.put(str.getBytes(CharsetUtil.UTF_8));
        //ByteBuffer bytebuffer = ByteBuffer.wrap(str.getBytes(CharsetUtil.UTF_8));

        byteBuffer.flip();

        channel.write(byteBuffer);

        fileOutputStream.close();
        channel.close();
    }
}
